package com.pic.mgt.unit.operation;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.file.io.FileOperation;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.unit.Unit;
import com.pic.mgt.utils.LogUtils;

public abstract class SimpleUnitOperation implements UnitOperation {
	protected FileOperation simpleFileOperation;

	@Override
	public void copyConfirmationFile(Unit unit, File destFolder)
			throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Unit '{}' operation: copying confirmation file '{}' to folder '{}'.",
								unit.getUnitName(), unit.getConfirmationFile()
										.getName(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
			
			this.simpleFileOperation.copyFileToFolder(unit
					.getConfirmationFile(), destFolder);
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Unit '{}' operation: copying confirmation file '{}' to folder '{}' completed.",
								unit.getUnitName(), unit.getConfirmationFile()
										.getName(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException(
						"Unit '" + unit.getUnitName() + "' operation: copying confirmation file '" +
						unit.getConfirmationFilename() + "' to folder '" +
						destFolder.getAbsolutePath() + "' failed.", e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}

	@Override
	public void moveConfirmationFile(Unit unit, File destFolder)
			throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Unit '{}' operation: moving confirmation file '{}' to folder '{}'.",
								unit.getUnitName(), unit.getConfirmationFile()
										.getName(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
			
			File newConfirmationFile = 
					this.simpleFileOperation.moveFileToFolder(unit
								.getConfirmationFile(), destFolder);
			unit.setConfirmationFile(newConfirmationFile);
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Unit '{}' operation: moving confirmation file '{}' to folder '{}' completed.",
								unit.getUnitName(), unit.getConfirmationFile()
										.getName(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException("Unit '" + unit.getUnitName()
						+ "' operation: moving confirmation file '"
						+ unit.getConfirmationFilename()
						+ "' to folder '" + destFolder.getAbsolutePath()
						+ "' failed.", e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}

	@Override
	public void removeConfirmationFile(Unit unit) throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Unit '{}' operation: removing confirmation file '{}'.",
								unit.getUnitName(), unit.getConfirmationFile()
										.getName());
			}
			// ********************** Log **********************
			
			this.simpleFileOperation.deleteFile(unit.getConfirmationFile());
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Unit '{}' operation: removing confirmation file '{}' completed.",
								unit.getUnitName(), unit.getConfirmationFile()
										.getName());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException("Unit '" + unit.getUnitName()
						+ "' operation: removing confirmation file '"
						+ unit.getConfirmationFilename() + "' failed.", e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}

	// --- getter and setter ---
	
	public void setSimpleFileOperation(FileOperation simpleFileOperation) {
		this.simpleFileOperation = simpleFileOperation;
	}

}
